//
//  HMEmoticonBottomView.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//定义一个参数(处理Bug)
private let tagNumber = 2000
// 按钮枚举
enum HMEmoticonBottomViewType: Int {
    // 默认
    case Default = 0 //0 不要轻易使用
    // emoji
    case Emoji = 1
    // 浪小花
    case Lxh = 2
}

//UIStackView 容器视图
class HMEmoticonBottomView: UIStackView {
    //记录
    var currentBtn: UIButton?
    //定义一个闭包 01
    var closure:((type:HMEmoticonBottomViewType) -> ())?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     // MARK: - 提供方法供外界调用
    func serupCurrentBtn(tag: Int){
    //得到btn
        let btn = viewWithTag(tag + tagNumber) as! UIButton
        self.selectBtn(btn)
    }
    
     // MARK: - 监听方法
    func btnCilck(btn: UIButton){
        self.selectBtn(btn)
    //执行闭包
        closure?(type:HMEmoticonBottomViewType(rawValue: btn.tag - tagNumber)!)
    }
    
     // MARK: - 选中设置判断
    func selectBtn(btn:UIButton){
        
        // 如果当前选中的button和这次的相同将不再设置
        if currentBtn == btn {
            return
        }
        //取消上个选中的按钮
        currentBtn?.selected = false
        //当前选中的
        btn.selected = true
        //赋值
        currentBtn = btn
   
    }
    
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        //填充方向
        axis = .Horizontal
        //填充方式
        distribution = .FillEqually
        
        
        addChildBtns("left", title: "默认",type:.Default)
        addChildBtns("mid", title: "Emoji",type:.Emoji)
        addChildBtns("right", title: "浪小花",type:.Lxh)
        
//    let btn1 = addChildBtns("left", title: "默认")
//    let btn2 = addChildBtns("mid", title: "Emoji")
//    let btn3 = addChildBtns("right", title: "浪小花")
//      
//        btn1.snp_makeConstraints { (make) in
//            make.leading.top.bottom.equalTo(self)
//            make.width.equalTo(btn2)
//        }
//        btn2.snp_makeConstraints { (make) in
//            make.top.bottom.equalTo(self)
//            make.leading.equalTo(btn1.snp_trailing)
//            make.width.equalTo(btn3)
//        }
//        btn3.snp_makeConstraints { (make) in
//            make.top.bottom.trailing.equalTo(self)
//            make.leading.equalTo(btn2.snp_trailing)
//        }
    
    }
    
    /**
     创建按钮
     
     - parameter imageName: 图片名
     - parameter title:     title
     */
    func addChildBtns(imageName:String, title:String , type:HMEmoticonBottomViewType){
       let btn = UIButton()
        btn.tag = type.rawValue + tagNumber
        
        btn.addTarget(self, action: "btnCilck:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        //默认情况下default是选中状态
        if type == .Default{
           btn.selected = true
           currentBtn = btn
        }
        
//        addSubview(btn)
        addArrangedSubview(btn)
      
    }
}
